package com.aos.eap.activity;

import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.WireInfoBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;

public class MFG_BackWireActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etWireId)
    FreeUI_ClearEditText etWireId;
    @BindView(R.id.etWireLotId)
    FreeUI_ClearEditText etWireLotId;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.tvWireThingId)
    TextView tvWireThingId;
    @BindView(R.id.tvThingTime)
    TextView tvThingTime;
    @BindView(R.id.tvWireLength)
    TextView tvWireLength;
    @BindView(R.id.tvLostTime)
    TextView tvLostTime;
    @BindView(R.id.tvDelayTime)
    TextView tvDelayTime;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private WireInfoBean wireInfoBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_back_wire;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etWireId, this, s -> MyUtils.setEditTextFocus(etWireLotId));
        MyUtils.setEditTextListener(etWireLotId, this, s -> requestWireInfo());
    }

    private void requestWireInfo() {
        String wireId = etWireId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(wireId)) {
            showToast("请扫描焊线卷轴编号", false);
            return;
        }
        String wireLotId = etWireLotId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(wireLotId)) {
            showToast("请扫描焊线批次号", false);
            return;
        }

        JsonObject map = new JsonObject();
        map.addProperty("WireID", wireId);
        map.addProperty("WireLotID", wireLotId);
        NetUtils.requestGetNet(this, "EMS_WireBackLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<WireInfoBean>>(
                new TypeToken<BaseResponseBean<WireInfoBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<WireInfoBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    wireInfoBean = response.body().getResponse().getBody();
                    setPageData(wireInfoBean);
                } else {
                    wireInfoBean = null;
                    setPageData(new WireInfoBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<WireInfoBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_WireBackLoad");
                wireInfoBean = null;
                setPageData(new WireInfoBean());
            }
        });
    }

    private void setPageData(WireInfoBean wireInfoBean) {
        tvEqpId.setText(wireInfoBean.getEqpID());
        tvWireThingId.setText(wireInfoBean.getWireMatID());
        tvThingTime.setText(wireInfoBean.getWireSendTime());
        tvWireLength.setText(String.valueOf(wireInfoBean.getWireLength()));
        tvLostTime.setText(wireInfoBean.getEqpWireRemainTime());
        tvDelayTime.setText(wireInfoBean.getEqpWireExpireTime());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("Wire退料");
        freeUI_titleView.setLeftListener(view -> showBackDialog(MFG_BackWireActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (wireInfoBean == null) {
                showToast("请先扫码获取wire信息", false);
                etWireId.setText("");
                MyUtils.setEditTextFocus(etWireId);
                return;
            }
            String wireId = etWireId.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(wireId)) {
                showToast("请扫描焊线卷轴编号", false);
                return;
            }
            String wireLotId = etWireLotId.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(wireLotId)) {
                showToast("请扫描焊线批次号", false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("WireID", wireId);
            map.addProperty("WireLotID", wireLotId);
            map.addProperty("EqpID", wireInfoBean.getEqpID());
            map.addProperty("WireMatId", wireInfoBean.getWireMatID());
            map.addProperty("WireSendTime", wireInfoBean.getWireSendTime());
            map.addProperty("WireLength", wireInfoBean.getWireLength());
            map.addProperty("WireRemainTime", wireInfoBean.getWireRemainTime());
            map.addProperty("WireExpireTime", wireInfoBean.getWireExpireTime());
            map.addProperty("IsNullReel", "N");
            map.addProperty("Remark", etRemark.getText().toString().trim());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_WireBack", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(MFG_BackWireActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_WireBack");
            }
        });
    }
}
